package com.google.code.kaptcha.text.impl;

import com.google.code.kaptcha.text.TextProducer;

import java.security.SecureRandom;
import java.util.Random;

public class MathTextProducer implements TextProducer {

    @Override
    public String getText() {
        Random random = new SecureRandom();
        int x = random.nextInt(10);
        int y = random.nextInt(10);

        int result = 0;
        StringBuilder sb = new StringBuilder();
        int operator = (int) Math.round(Math.random() * 2);
        if (operator == 0) {
            result = x * y;
            sb.append(x);
            sb.append("×");
            sb.append(y);
        } else if (operator == 1) {
            if (!(x == 0) && y % x == 0) {
                result = y / x;
                sb.append(y);
                sb.append("/");
                sb.append(x);
            } else {
                result = x + y;
                sb.append(x);
                sb.append("+");
                sb.append(y);
            }
        } else if (operator == 2) {
            if (x >= y) {
                result = x - y;
                sb.append(x);
                sb.append("-");
                sb.append(y);
            } else {
                result = y - x;
                sb.append(y);
                sb.append("-");
                sb.append(x);
            }
        } else {
            result = x + y;
            sb.append(x);
            sb.append("+");
            sb.append(y);
        }
        sb.append("=?@" + result);
        return sb.toString();
    }
}
